/**
 * Dialog cho phep go ten map de save lai
 */
package view.drag;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import model.database.LevelDB;
import model.database.MapDB;
import model.entity.Levels;
import model.entity.Maps;

/**
 * @author heroandtn3
 * @date Nov 6, 2012
 */
public class SaveMapDlg extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton butSave;
	private JButton butCancel;
	private JLabel lbNotice;
	private JTextField tfMapName;
	private JLabel lbMapName;
	private int[][] matrix;
	private Maps map = null;
	private DragMapFrame dmf;

	/**
	 * 
	 */
	public SaveMapDlg(Frame owner, boolean modal) {
		// TODO Auto-generated constructor stub
		super(owner, modal);
		this.dmf = (DragMapFrame) owner;
		setAlwaysOnTop(true);
		setResizable(false);
		initGUI();
		setLocationRelativeTo(null);
		setVisible(false);
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				getContentPane().setBackground(new java.awt.Color(0,138,0));
				{
					lbMapName = new JLabel();
					getContentPane().add(lbMapName);
					lbMapName.setText("Enter name for new map");
					lbMapName.setBounds(26, 124, 174, 25);
					lbMapName.setFont(new java.awt.Font("Segoe UI",0,16));
					lbMapName.setForeground(new java.awt.Color(255,255,255));
				}
				{
					tfMapName = new JTextField();
					getContentPane().add(tfMapName);
					tfMapName.setBounds(237, 127, 216, 22);
				}
				{
					butSave = new JButton("Save");
					getContentPane().add(butSave);
					butSave.setBounds(351, 180, 102, 26);
					butSave.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for butSave.mouseClicked
							if (tfMapName.getText().length() > 0) {
								if (map != null) {
									map.setName(tfMapName.getText());
									MapDB mDB = new MapDB();
									mDB.insertMap(map);
									int mapID = mDB.getLastMapID();
									Levels level = new Levels();
									level.setName("Level of " + mapID);
									level.setMatrix(matrix);
									LevelDB lvDB = new LevelDB();
									lvDB.insertLevel(level, mapID);
									setVisible(false);
									dmf.getUserHome().initListMap();
									dmf.setVisible(false);
									
								}
							} else {
								lbNotice.setText("Please enter map name");
							}
							
						}
					});
				}
				{
					butCancel = new JButton("Cancel");
					getContentPane().add(butCancel);
					butCancel.setBounds(238, 180, 102, 26);
					butCancel.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for butCancel.mouseClicked
							setVisible(false);
						}
					});
				}
				{
					lbNotice = new JLabel();
					getContentPane().add(lbNotice);
					lbNotice.setBounds(238, 227, 215, 31);
				}
			}
			{
				this.setSize(501, 322);
			}
		} catch(Exception e) {
			//e.printStackTrace();
		}
	}

	/**
	 * @return the matrix
	 */
	public int[][] getMatrix() {
		return matrix;
	}

	/**
	 * @param matrix the matrix to set
	 */
	public void setMatrix(int[][] matrix) {
		this.matrix = matrix;
	}

	/**
	 * @param map the map to set
	 */
	public void setMap(Maps map) {
		this.map = map;
	}

}
